import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import NewsManage from '../views/NewsManage.vue'
import UserManage from '../views/UserManage.vue'
import SystemNoticeManage from '../views/SystemNoticeManage.vue'
import advertisersManage from '../views/advertisersManage.vue'
import adCampaignsManage from '../views/adCampaignsManage.vue'
import UserFeedback from '../views/UserFeedback.vue'
import incomeStatistic from  '../views/IncomeStatistics.vue'
import NewsReview from '../views/NewsReview.vue'


const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    name: 'Home',
    component: Home,
    meta: { requiresAuth: true },
    children: [
      {
        path: 'newsreview',
        name: 'NewsReview',
        component: NewsReview,
        meta: {
          title: '新闻审核',
          requiresAuth: true,
          permissions: ['newsreview']
        }
      },
      {
        path: 'news',
        name: 'NewsManage',
        component: NewsManage,
        meta: { requiresAuth: true }
      },
      {
        path: 'users',
        name: 'UserManage',
        component: UserManage,
        meta: { requiresAuth: true }
      },
      {
        path: 'systemNotice',
        name: 'SystemNotice',
        component: SystemNoticeManage,
        meta: {
          requiresAuth: true,
          title: '系统通知管理'
        }
      },
      {
        path:"readNotice",
        name: "ReadNotice",
        component: () => import("../views/ReadNotice.vue"),
        meta:{ requiresAuth: true}
      },
      {
        path: "comments",
        name: "CommentManage",
        component: () => import("../views/CommentManage.vue"),
        meta: {
          requiresAuth: true,
          title: "评论管理",
        },
      },
      {
        path: "reports",
        name: "ReportManage",
        component: () => import("../views/ReportManagement.vue"),
        meta: {
          requiresAuth: true,
          title: '评论管理'
        }
      },
      {
        path: 'advertisers',
        name: 'advertisersManage',
        component: advertisersManage,
        meta: { requiresAuth: true }
      },
      {
        path: 'adcampaigns',
        name: 'adCampaignsManage',
        component: adCampaignsManage,
        meta: { requiresAuth: true }
      },
      {
        path: 'feedback',
        name: 'UserFeedback',
        component: UserFeedback,
        meta: { requiresAuth: true,
          title:'用户反馈'
         }
      },
      {
        path: 'violated',
        name: 'ViolatedList',
        component: () => import('../views/ViolatedList.vue'),
        meta: {
          requiresAuth: true,
          title: '违规内容管理'
        }
      },
      {
        path: 'income',
        name: 'incomeStatistic',
        component: incomeStatistic,
        meta: { requiresAuth: true }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')

  if (to.meta.requiresAuth && !token) {
    // 需要登录但未登录，重定向到登录页
    next('/login')
  } else if (to.path === '/login' && token) {
    // 已登录但访问登录页，重定向到首页
    next('/')
  } else {
    // 设置页面标题
    document.title = to.meta.title || '后台管理系统'
    next()
  }
})

export default router
